package models;
import java.text.Format;
import java.text.ParseException;
import java.util.Date;
import javax.persistence.*;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Page;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import controllers.JsonDateFormatter;
import controllers.JsonDatetimeFormatter;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import controllers.Helper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
import models.Vehicle;
import models.Driver;
@Entity
@Table(name="add_oil2")
public class Add_oil2 extends Model {
	private static final long serialVersionUID = 1L;
	@Id
	public Integer id; //编号
	@ManyToOne
	public Vehicle vehicle; //车牌号
	@ManyToOne
	public Driver name; //司机
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date addoil_date; //加油日期
	
	public String addoli_place; //加油地点
	
	public Integer addoil_number; //加油量
	
	public Double addoil_money; //加油金额
	
	public String oil_bill; //加油凭据
	
	public String oilbill_number; //单据号
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date write_date; //登记日期
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date check_date; //复核日期
	@JsonSerialize(using=JsonDatetimeFormatter.class)
	public Date check_time; //复核时间
	
	public String check_name; //复核人
	public static Model.Finder<Integer, Add_oil2> find = new Model.Finder<Integer, Add_oil2>(Integer.class, Add_oil2.class);

	public static Page<Add_oil2> page(int page, int pageSize, String sortBy, String order, String [] filterColum, String[] filter) {
		ExpressionList<Add_oil2> exp = find.where();
		for(int i= 0; i< filterColum.length; i++) {
			String[] tokens = filter[i].split("\\|", 3);
			if (tokens.length == 0) {
				continue;
			}
			String fieldtype = tokens[0];
			if (tokens.length == 3) { //two values, range search
				if (tokens[1].isEmpty() || tokens[2].isEmpty() ) {
					continue;
				}
				Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
				Object obj1 = Helper.stringToObject(fieldtype, tokens[2]);
				exp = exp.between(filterColum[i], obj0, obj1);
			} else if (tokens.length == 2) { //one value search
				if (tokens[1].isEmpty()) {
					continue;
				}
				if (fieldtype.equalsIgnoreCase("String")) {
					exp = exp.ilike(filterColum[i], "%" + tokens[1] + "%");
				} else {
					Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
					exp = exp.eq(filterColum[i], obj0);
				}
			}
		}
		return exp.orderBy(sortBy + " " + order).findPagingList(pageSize).setFetchAhead(false).getPage(page);
	}
	public void setId(Integer d) {
		id= d;
	}
	public Integer getId() {
		return id;
	}
	public void setVehicle(Vehicle d) {
		vehicle= d;
	}
	public Vehicle getVehicle() {
		return vehicle;
	}
	public void setName(Driver d) {
		name= d;
	}
	public Driver getName() {
		return name;
	}
	public void setAddoil_date(Date d) {
		addoil_date= d;
	}
	public Date getAddoil_date() {
		return addoil_date;
	}
	public void setAddoli_place(String d) {
		addoli_place= d;
	}
	public String getAddoli_place() {
		return addoli_place;
	}
	public void setAddoil_number(Integer d) {
		addoil_number= d;
	}
	public Integer getAddoil_number() {
		return addoil_number;
	}
	public void setAddoil_money(Double d) {
		addoil_money= d;
	}
	public Double getAddoil_money() {
		return addoil_money;
	}
	public void setOil_bill(String d) {
		oil_bill= d;
	}
	public String getOil_bill() {
		return oil_bill;
	}
	public void setOilbill_number(String d) {
		oilbill_number= d;
	}
	public String getOilbill_number() {
		return oilbill_number;
	}
	public void setWrite_date(Date d) {
		write_date= d;
	}
	public Date getWrite_date() {
		return write_date;
	}
	public void setCheck_date(Date d) {
		check_date= d;
	}
	public Date getCheck_date() {
		return check_date;
	}
	public void setCheck_time(Date d) {
		check_time= d;
	}
	public Date getCheck_time() {
		return check_time;
	}
	public void setCheck_name(String d) {
		check_name= d;
	}
	public String getCheck_name() {
		return check_name;
	}
	public static Add_oil2 findById(Integer d) {
		return find.byId(d);
	}
	public static String getIdJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Add_oil2 v: find.all()) {
			node.put(v.getId().toString(), v.getId().toString()); 
		}
		return node.toString();
	}
}
